/*
 * Created by David Hoel on 7/27/14.
 * This was created to test the dominion game by playing full games of dominion to
 * ensure that the code executes correctly.  The way that I approached this was to 
 * initiate a large number of games with a random assortment of kingdom cards and
 * a random number of players.  I then would play cards in a random fashion to test
 * that the game would continue without a glitch.  However I found that when the 
 * feast card was played in this way there was no way to generate new input, 
 * if the original input did not allow the card to complete the transaction.
 * This caused the feast card to cause an infinite loop.
*/
#include "dominion.h"
#include <stdio.h>
#include "rngs.h"
#include <stdlib.h>
#include <math.h>
#include <unistd.h>

#define NUM_CARDS 20

int main (int argc, char** argv) {
  //Decleration of variables
  FILE* pFile;
  struct gameState G;
  int k[10];
  double rand;  
  int money, turn, gameNumber;
  int choice;
  int i, j;
  int new;
  int players;
  int actions[10];
  enum CARD card;
  pFile = fopen("gameResults.out", "w");
  printf("Starting Tests:\n");
  //Loop to play 2000 games.
  for(gameNumber = 1; gameNumber < 2000; gameNumber++){
  money = 0;
  players = 2 + Random() * (MAX_PLAYERS - 1);
  //Randomly assign kingdom cards.
  for (i = 0; i < 10; i++){
     card = 7 + floor(Random() * NUM_CARDS);
     new = 1;
     for (j = 0; j < i; j++){
	if(card == k[j]){ //Ensure no duplicate kingdom cards
	   i--;
	   new = 0;
     }
     if (new == 1)
     	k[i] = card;
     }
   } 
  turn = 1;
  if(gameNumber == 500 || gameNumber == 1500){ //prints gamenumbers 500 and 1500
     printf ("Starting game: %d\n", gameNumber);
  }
  fprintf(pFile, "Game Number: %d\n", gameNumber);
  initializeGame(players, k, atoi(argv[1]), &G);
  while (!isGameOver(&G)) {
    fprintf(pFile, "Player: %d\nTurn: %d\n", G.whoseTurn, turn);
    turn++;
     j = 0;
    money = 0;
    fprintf(pFile, "Hand: ");
    for (i = 0; i < numHandCards(&G); i++) {
      fprintf(pFile, "Card: %d\n", handCard(i, &G));
       if (handCard(i, &G) == copper)
    money++;
      else if (handCard(i, &G) == silver)
    money += 2;
        else if (handCard(i, &G) == gold)
    money += 3;
      if (handCard(i, &G) > 6 && handCard(i, &G) != 9){
	 actions[j] = i;
      	 j++;
      }
    }
    card = floor(j * Random());
    choice = floor(Random() * 10);
    fprintf(pFile, "Play card: %d From position: %d\n", handCard(actions[card], &G), actions[card]);
    fprintf(pFile, "Choices: %d\n", choice);
    if(j > 0)
    	playCard(actions[card], choice, choice, choice, &G);
    card = floor(Random() * 27);
    fprintf(pFile, "Trying to purchase card: %d\n", card);
    if(buyCard(card, &G) == -1)
       fprintf(pFile, "Purchase failed.\n");
    else
       fprintf(pFile, "Purchase success.\n");

    if (money >= 8) {
        fprintf(pFile, "bought province\n"); 
        buyCard(province, &G);
      }
    else if (money >= 6) {
        fprintf(pFile, "bought gold\n"); 
        buyCard(gold, &G);
      }
    else if (money >= 3) {
        fprintf(pFile, "bought silver\n"); 
        buyCard(silver, &G);
      }
   
      endTurn(&G);      
  } // end of While
  
  if(G.numPlayers == 2)
  	fprintf (pFile, "Player 0: %d\nPlayer 1: %d\n", scoreFor(0, &G), scoreFor(1, &G));
  if(G.numPlayers == 3)
  	fprintf (pFile, "Player 0: %d\nPlayer 1: %d\nPlayer 2: %d\n", scoreFor(0, &G), scoreFor(1, &G), scoreFor(2, &G));
  if(G.numPlayers == 4)
  	fprintf (pFile, "Player 0: %d\nPlayer 1: %d\nPlayer 2: %d\nPlayer 3: %d\n", scoreFor(0, &G), scoreFor(1, &G), scoreFor(3, &G), scoreFor(4, &G));
   

  }  
  printf("Finished last game.\nAll tests cleared.\n");
  fclose(pFile);
  return 0;
}
